const fs = require("fs");
let source = require("./source.json");
let config = require("./apicodegen.js");
// import { config } from "./apicodegen";
let result = "";
let DataTypes = {
  integer: "number",
  number: "number",
  string: "string",
  boolean: "boolean",
  array: "Array",
  object: "object",
};

console.log(config);

config.projects.forEach((project) => {});

Object.keys(source.paths).forEach((key) => {
  let path = key;
  let value = source.paths[key];
});

/*
export function getFilterByCode(paramsOrData: any, headers, options): Promise<any> {
  return request({
    url: "/sku_on_sale/getFilterByCode",
    method: "get",
    data: paramsOrData,
    params: paramsOrData,
    ...options,
  });
}
*/
function genAllPath(pathsObj) {
  fnstr = ``;
  Object.keys(pathsObj).forEach((key) => {
    let _description, _fnName, _url, _method, apiDescribe;
    _url = key;
    _fnName = _url.split("/").reverse()[0];
    Object.keys(pathsObj[key]).forEach((method) => {
      _method = method;
      apiDescribe = pathsObj[key][method];
    });
    _description = apiDescribe.summary;
    let { queryArr, bodyArr, headerArr } = buildParameter(
      apiDescribe.parameters,
      _fnName
    );
    let queryStr = "";
    let bodyStr = "";
    if (queryArr.length > 2) {
      queryArr.forEach((query) => {
        queryStr += `${query.name}${query.required}: ${query.type};
`;
      });
      queryStr = `
interface ${_fnName}Query {
  ${queryStr}}`;
    } else {
      queryStr = queryArr.map((query) => {
        return `${query.name}${query.required}: ${query.type},`;
      });
    }
    if (bodyArr) {
      bodyArr.forEach((query) => {
        bodyStr += `${query.name}${query.required}: ${query.type};
`;
      });
      bodyStr = `
interface ${_fnName}Body {
  ${bodyStr}}`;
    }

    fnstr += `
/* ${_description} */
${queryArr.length > 2 ? queryStr : ""}
${bodyStr}
export function ${_fnName}(${
      queryArr.length > 2 ? "query: " + _fnName + "Query" : queryStr
    } data: ${_fnName}Body, options): Promise<any> {
  return request({
    url: "${_url}",
    method: "${_method}",
    data: data,
    params: ${queryArr.length > 2 ? "query" : queryArr.map((v) => v.name)},
    ...options,
  });
}
`;
  });
  writeFile("fn.ts", fnstr);
}

/*
interface header {

}
*/
function buildParameter(parameters) {
  let queryArr = [];
  let bodyArr = [];
  let headerArr = [];
  parameters.forEach((param) => {
    let required = param.required ? "" : "?";
    if (param.in === "query") {
      queryArr.push({
        name: param.name,
        required: required,
        type: DataTypes[param.type],
      });
    } else if (param.in === "header") {
      headerArr.push({
        name: param.name,
        required: required,
        type: DataTypes[param.type],
      });
    } else if (param.in === "body") {
      bodyArr.push({
        name: param.name,
        required: required,
        type: getSchema(param.schema),
      });
    }
  });
  return {
    queryArr,
    bodyArr,
    headerArr,
  };
}

function getSchema(schema) {
  if (schema.type === "array") {
    return `Array<${schema.items.originalRef}>`;
  } else {
    return `${schema.originalRef}`;
  }
}
function writeFile(path, content) {
  fs.writeFile(path, content, function (err) {
    if (err) {
      return console.error(err);
    }
    console.log("数据写入成功！");
    fs.readFile(path, function (err, data) {
      if (err) {
        return console.error(err);
      }
      console.log("异步读取文件数据: ");
      // data.toString()
    });
  });
}

genAllPath(source.paths);
